package com.lihua.newideas.controller;

import com.alibaba.fastjson.JSON;
import com.github.pagehelper.PageInfo;
import com.lihua.newideas.pojo.Article;
import com.lihua.newideas.service.impl.ArticleServiceImpl;
import com.lihua.newideas.service.impl.MyUserServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;

//主页、初始化
@Controller
public class IndexController {


    @Autowired
    MyUserServiceImpl userServiceImple;

    @Autowired
    ArticleServiceImpl articleService;

    @RequestMapping({"/","/index"})
    public String index(Model model, @RequestParam(value = "page", defaultValue = "1") int page,
                        @RequestParam(value = "limit", defaultValue = "8") int limit){
        PageInfo<Article> articlePageInfo = articleService.queryArticleByArticleStatus(0, page, limit);
        model.addAttribute("articlePageInfo",articlePageInfo);
        return "index";
    }
//    跳转登录页面
    @RequestMapping("/toLogin")
    public String toLogin(){
        return "userLogin/login";
    }


    @ResponseBody
    @RequestMapping("/toError")
    public String toError(){
        System.out.println("密码错误！");
        return "密码错误";
    }

    @RequestMapping("/test")
    public String test(){
        return "test";
    }

//    没有权限访问
    @RequestMapping("/notAuthority")
    public String notAuthority(){
        return "/error/403";
    }

//    管理员界面
    @RequestMapping("/admin")
    public String admin(){
        return "admin/admin";
    }

    /*
     * 文章详情,根据id查询
     * */
    @RequestMapping("/articleDetails/{id}")
    public String articleDetails(@PathVariable String id, Model model){
        System.out.println(id);
        long id1 = Long.valueOf(id);
        Article article = articleService.queryArticleById(id1);
        model.addAttribute("article",article);
        return "article_details";
    }

    /*
    * 搜索文章
    * */
    @RequestMapping("/searchArticle/{articleTitle}")
    public String searchArticle(@PathVariable("articleTitle") String articleTitle ,Model model){
        System.out.println(articleTitle);
        List<Article> articles = articleService.queryArticleByArticleTitle(articleTitle);
//        System.out.println(articles);
        model.addAttribute("articles",articles);
        return "search_article";
    }




}
